package com.wilson.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wilson.commonutils.R;
import com.wilson.eduservice.entity.EduTeacher;
import com.wilson.eduservice.entity.vo.TeacherQuery;
import com.wilson.eduservice.service.EduTeacherService;
import com.wilson.servicebase.config.exceptionHandler.GuliException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author wilson
 * @since 2023-12-17
 */
@CrossOrigin//解决跨域
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    //把service注入
    @Autowired
    private EduTeacherService eduTeacherService;

    //rest风格
    //http://localhost:8001/eduservice/teacher/findAll
//    @GetMapping("findAll")
//    public List<EduTeacher> findAll(){
//        //调用service的查询所有操作
//        List<EduTeacher> list = eduTeacherService.list(null);
//        return list;
//    }
    //统一返回格式
    @GetMapping("findAll")
    public R findAll(){
        //调用service的查询所有操作
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }


    //逻辑删除
    @DeleteMapping("removeTeacherById/{id}")
    public R removeTeacher(@PathVariable String id){
//        boolean flag = eduTeacherService.removeById(id);
//        if (flag){
//            return R.ok();
//        }else{
//            return R.error();
//        }
        return eduTeacherService.removeById(id)?R.ok():R.error();
    }

    //分页查询
    @ApiOperation(value = "分页讲师列表")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,@PathVariable long limit){
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        eduTeacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

//        Map map = new HashMap();
//        map.put("total",total);
//        map.put("rows",records);
//        return R.ok().data(map);

        return R.ok().data("total",total).data("rows",records);
    }

    //条件查询带分页
    //RequestBody必须为PostMapping
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        //构建Page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }

        //排序,根据时间降序排序
        wrapper.orderByDesc("gmt_create");

        eduTeacherService.page(pageTeacher,wrapper);

        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        return R.ok().data("total",total).data("rows",records);
    }

    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        return eduTeacherService.save(eduTeacher)?R.ok():R.error();
    }

    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){

//        try{
//            int a = 10/0;
//        }catch (Exception e){
//            //执行自定义异常
//            throw new GuliException(20001,"自定义异常");
//        }

        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }

    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        return eduTeacherService.updateById(eduTeacher)?R.ok():R.error();
    }

}

